/**
 * 年度费用预算维护作业 - 编辑页面
 * @author ganjia
 * @date 2019/10/25
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            form: {
                GWFB003_NAME:null,
                GWFB004_NAME:null,
                GWFB005_NAME:null,
                GWFB100_NAME:null
            },
            rules: {
                GWFBNO: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFB001: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFB002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFB003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFB004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFB005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWFBACTI: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions:{
                sys_y_n:app.datas_("crm_gwei006_conf")
            },
        },
        callObj,
        pageVue,
        layOpenIndex
    ;

    var Gwft100Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwft100_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: '/common/_open_window'
                }]
            }, win);
        },

        editData: function () {
            app.ajax({
                xurl: 'crm/gwft100/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '年度费用预算信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwft100').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        addData: function () {
            app.ajax({
                xurl: 'crm/gwft100/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '年度费用预算信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwft100').reloadTableCurr();
                        self._closeTpl();
                    }
                }
            });
        },

        _call: function (obj) {
            pageVue.$refs['form'].resetFields();
            layOpenIndex = obj.layOpenIndex;
            if (obj.is_add) {
                vueData.isAdd=true;
                // app.log(obj);
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        GWFBACTI:'N'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = obj;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['GWFB_T'], vueData.form);
            pageVue = new Vue({
                el: '#gwft100_edit',
                data: vueData,
                methods: {
                    openKh:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwak001',
                                    columnList: [
                                        {label: '客户编号', prop: 'GWAK001', width: '150'},
                                        {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                        {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                    ]
                                },
                                cb:function (data) {
                                    vueData.form.GWFB004_NAME = data.GWAKL003;
                                    vueData.form.GWFB004 = data.GWAK001;
                                    vueData.form.GWFB100 = data.GWAK008;
                                    vueData.form.GWFB100_NAME = data.GWAK008_NAME;
                                    app.ajax({
                                        xurl: 'crm/gwft300/getHuiLv',
                                        xdata:{GWEK001: data.GWAK008, GWEK002: new Date().Format("yyyyMM")},
                                        cb: function (result) {
                                            if(result.ok) {
                                                var data = result.data.data;
                                                vueData.form.GWFB101 = data.GWEK005;
                                            }
                                        }
                                    });
                                }
                            },
                            index:0,
                            openOpts: {
                                title: '选择客户',
                                area:['600px', '470px']
                            }
                        });
                    },
                    openBz: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwac001',
                                    columnList: [
                                        {label: '币种编号', prop: 'GWAC001', width: '150'},
                                        {label: '说明', prop: 'GWACL003', width: '150'},
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWFB100 = data.GWAC001;
                                    vueData.form.GWFB100_NAME = data.GWACL003;
                                    vueData.form.GWFB102 = '';
                                    vueData.form.GWFB103 = '';
                                    app.ajax({
                                        xurl: 'crm/gwft300/getHuiLv',
                                        xdata:{GWEK001: data.GWAC001, GWEK002: new Date().Format("yyyyMM")},
                                        cb: function (result) {
                                            if(result.ok) {
                                                var data = result.data.data;
                                                vueData.form.GWFB101 = data.GWEK005;
                                            }
                                        }
                                    });
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择币种信息',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    openGWAD002: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwad002',
                                    columnList: [
                                        {label: '区域编号', prop: 'GWADL002', width: '150'},
                                        {label: '区域名称', prop: 'GWADL004', width: '150'},
                                        {label: '助记码', prop: 'GWADL005', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWFB003 = data.GWADL002;
                                    vueData.form.GWFB003_NAME = data.GWADL004;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择区域',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    openGWAO001: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwao001',
                                    columnList: [
                                        {label: '产品系列编号', prop: 'GWAO001', width: '150'},
                                        {label: '说明', prop: 'GWAOL003', width: '150'},
                                        {label: '助记码', prop: 'GWAOL004', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWFB005 = data.GWAO001;
                                    vueData.form.GWFB005_NAME = data.GWAOL003;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择品类',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    changeGwfb101:function () {
                        vueData.form.GWFB103 = math.mul(vueData.form.GWFB102, vueData.form.GWFB101);
                    },
                    changeGwfb102:function () {
                        vueData.form.GWFB103 = math.mul(vueData.form.GWFB102, vueData.form.GWFB101);
                    },
                    changeGwfb103:function () {
                        vueData.form.GWFB102 = math.div(vueData.form.GWFB103, vueData.form.GWFB101);
                    },
                    cancel: function () {
                        layer.close(layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    }
                },
                watch: {
                    'form.GWFB003_NAME': function () {
                        if (vueData.form.GWFB003_NAME == '') {
                            vueData.form.GWFB003 = '';
                        }
                    },
                    'form.GWFB004_NAME': function () {
                        if (vueData.form.GWFB004_NAME == '') {
                            vueData.form.GWFB004 = '';
                        }
                    },
                    'form.GWFB005_NAME': function () {
                        if (vueData.form.GWFB005_NAME == '') {
                            vueData.form.GWFB005 = '';
                        }
                    },
                    'form.GWFB100_NAME': function () {
                        if (vueData.form.GWFB100_NAME == '') {
                            vueData.form.GWFB100 = '';
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwft100_edit');
                }
            });
        },

        _lazy: function () {
            this._super();
        }
    });

    return new Gwft100Edit();

});
